package view;
import com.toedter.calendar.JDateChooser;

import controller.Sistema;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import model.Consorcio;
import model.Departamento;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JBuscarReclamo extends javax.swing.JFrame {
	private JComboBox jComboConsorcios;
	private JButton btnSelecccionar;
	private JButton btnCancelar;
	private JLabel jLabel3;
	private JDateChooser jdtDesde;
	private JTable jTable1;
	private JScrollPane jScrollPane1;
	private JLabel jLabel4;
	private JDateChooser jdtHasta;
	private JPanel jPanel1;
	private JButton btnBuscar;
	private JComboBox jComboUF;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private static JBuscarReclamo instancia;
	
	private Vector<Consorcio> consorcios;
	private Vector<Departamento> departamentos;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JBuscarReclamo inst = new JBuscarReclamo();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public JBuscarReclamo() {
		super();
		initGUI();
	}
	
	public static JBuscarReclamo getInstancia(){
		if (instancia == null)
			instancia = new JBuscarReclamo();
		return instancia;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Buscar Reclamo");
			{

				consorcios = Sistema.getInstancia().getConsorcios();
				//Armar texto							
				Vector<String> nombres = new Vector<String>();
				if(consorcios!=null){
				for (int i=0;i<consorcios.size();i++){
					nombres.add(consorcios.elementAt(i).getNombre());
				}
				}
				
				ComboBoxModel jComboConsorciosModel = 	new DefaultComboBoxModel(nombres);
				jComboConsorcios = new JComboBox();
				getContentPane().add(jComboConsorcios);
				jComboConsorcios.setModel(jComboConsorciosModel);
				jComboConsorcios.setBounds(130, 28, 284, 23);
				jComboConsorcios.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
												
						departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());	
						Vector<String> unidades = new Vector<String>();
						if (departamentos != null){
							for (int i=0;i<departamentos.size();i++){
								unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
							}
							jComboUF.removeAllItems();
							for(int i=0; i<unidades.size();i++){
								jComboUF.addItem(unidades.elementAt(i));
								}
							
						}
						
						
					}
				});
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Consorcio");
				jLabel1.setBounds(20, 31, 98, 16);
				jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				btnSelecccionar = new JButton();
				getContentPane().add(btnSelecccionar);
				btnSelecccionar.setText("Seleccionar");
				btnSelecccionar.setBounds(144, 329, 105, 23);
				btnSelecccionar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						int fila = jTable1.getSelectedRow();
						String valor = jTable1.getModel().getValueAt(fila, 0).toString();
						JRegistrarPresupuesto.getInstancia().setNumeroReclamo(Integer.valueOf(valor));
						//System.out.print("el valor es "+valor);
						dispose();
						
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(260, 329, 85, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Unidad Funcional");
				jLabel2.setBounds(8, 66, 110, 16);
				jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());
				//Armar texto
				Vector<String> unidades = new Vector<String>();
				if (departamentos != null){
					for (int i=0;i<departamentos.size();i++){
						unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
					}
					
				}
							
				ComboBoxModel jComboUFModel =new DefaultComboBoxModel(unidades);
				jComboUF = new JComboBox();
				getContentPane().add(jComboUF);
				jComboUF.setModel(jComboUFModel);
				jComboUF.setBounds(132, 63, 282, 23);
			}
			{
				btnBuscar = new JButton();
				getContentPane().add(btnBuscar);
				btnBuscar.setText("Buscar");
				btnBuscar.setBounds(306, 133, 108, 23);
				btnBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
												
						Vector<String> columNames = new Vector<String>(4);
						columNames.addElement("Numero");
						columNames.addElement("Fecha");
						columNames.addElement("Propietario");
						columNames.addElement("Reclamo");
						
						DefaultTableModel model = new DefaultTableModel(Sistema.getInstancia().obtenerDatosReclamo(jComboConsorcios.getSelectedItem().toString(), Integer.valueOf(jComboUF.getSelectedItem().toString()),jdtDesde.getCalendar(), jdtHasta.getCalendar()), columNames);
						//DefaultTableModel model = new DefaultTableModel(Sistema.getInstancia().obtenerDatosData(jComboConsorcios.getSelectedItem().toString(), numero), colNames);
						jTable1.setModel(model);
																			
					}
				});
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				jPanel1.setBounds(20, 172, 458, 151);
				jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				jPanel1.setLayout(null);
				{
					jScrollPane1 = new JScrollPane();
					jPanel1.add(jScrollPane1);
					jScrollPane1.setBounds(7, 6, 445, 138);
					{
						String[] colNames = {"Numero","Fecha", "Propietario", "Reclamo"};
						
						TableModel jTable1Model = new DefaultTableModel(null,colNames);
						jTable1 = new JTable();
						jScrollPane1.setViewportView(jTable1);
						jTable1.setModel(jTable1Model);
					}
				}
			}
			{
				Calendar fecha = Calendar.getInstance();
				jdtDesde = new JDateChooser();
				jdtDesde.setCalendar(fecha);
				getContentPane().add(jdtDesde);
				jdtDesde.setBounds(132, 98, 108, 23);
			}
			{
				Calendar hoy = Calendar.getInstance();
				jdtHasta = new JDateChooser();
				jdtHasta.setCalendar(hoy);
				getContentPane().add(jdtHasta);
				jdtHasta.setBounds(306, 98, 108, 23);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Fecha desde");
				jLabel3.setBounds(20, 101, 97, 16);
				jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Hasta");
				jLabel4.setBounds(257, 101, 42, 16);
				jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			pack();
			this.setSize(516, 401);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
